#!/usr/bin/env python
# -*- coding: utf-8 -*-

import URLschr
import os

class URLparser:
	def __init__(self,patent):
		self.__URLschr	= URLschr.URLsrch(patent)
		self.__info_lst	= None
		self.FstURL	= None
		self.PGnum	= None
		self.ext_name	= None
		self.targetHead	= None

	def get_info(self):
		self.__URLschr.search()
		self.__info_lst	= self.__URLschr.info_lst
		self.PGnum	= self.__info_lst["totalpage"]
		self.FstURL	= self.__info_lst["tifpath"]
		self.basename	= self.FstURL.split(r"/")[-1]

		self.dir_name	= self.FstURL.replace(self.basename,'')
		self.file_name	= self.basename.split(".")[0]
		self.ext_name	= os.path.basename(self.FstURL).split(".")[-1]
	

	def crt_targetHead(self):
		if self.__info_lst != None:
			pass
		else:
			self.get_info()

		self.targetHead	= self.dir_name+("0"*(len(self.file_name)-len(self.PGnum)))

	
	def get_PGnum(self):
		if self.__info_lst != None:
			pass
		else:
			self.get_info()
		return self.PGnum
	
	def get_extname(self):
		if self.targetHead != None:
			pass
		else:
			self.crt_targetHead()

		return self.ext_name

	def get_downinfo(self):
		if self.targetHead != None:
			pass
		else:
			self.crt_targetHead()
		
		return dict(	downHead=self.targetHead,
				totalpage=self.PGnum,
				extname=self.ext_name)



if __name__ == '__main__':
	testParser	=	URLparser("200410043401")

	testParser.crt_targetHead()

	print "First URL: ",testParser.FstURL
	print "basename: ",testParser.basename
	print "dirname: ",testParser.dir_name
	print "filename: ",testParser.file_name
	print "extname: ",testParser.ext_name
	print "targetHead: ",testParser.targetHead
